package com.frost.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
//@Component
@Aspect
public class AspectDemo {

    @Pointcut("execution(* com.frost.aop.controller.*.*(..))")
    public void pt() {}

    @Before("pt()")
    public void doBefore() {
        log.info("执行AspectDemo.doBefore 方法");
    }

    @After("pt()")
    public void doAfter() {
        log.info("执行AspectDemo.doAfter 方法");
    }

    @AfterReturning("execution(* com.frost.aop.controller.*.*(..))")
    public void doAfterReturning() {
        log.info("执行AspectDemo.doAfterReturning 方法");
    }

    @AfterThrowing("execution(* com.frost.aop.controller.*.*(..))")
    public void doAfterThrowing() {
        log.info("执行AspectDemo.doAfterThrowing 方法");
    }

    @Around("execution(* com.frost.aop.controller.*.*(..))")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        log.info("执行AspectDemo.doAround 目标方法前");
        Object result = null;
        try {
            joinPoint.proceed();
        }catch (Throwable e) {
            log.error(joinPoint.toShortString() + "发生异常, e: " + e);
//            throw new RuntimeException(e);
        }
        log.info("执行AspectDemo.doAround 目标方法后");
        return result;
    }
}
